<template>
  <div v-loading="loading" class="order-manage-main">
    <div class="top-container">
      <div class="filter-container">
        <el-form
          ref="filterForm"
          :model="filterForm"
          :rules="rules"
          inline
          label-width="120px"
          size="small"
        >
          <el-form-item prop="date" label="下单时间：">
            <el-date-picker
              v-model="filterForm.createDate"
              :default-time="['00:00:00', '23:59:59']"
              type="datetimerange"
              range-separator="~"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item prop="orderNo" label="订单编号："
            ><el-input v-model="filterForm.orderNo" maxlength="50"
          /></el-form-item>
          <el-form-item prop="customerName" label="下单人昵称："
            ><el-input v-model="filterForm.customerName" maxlength="50"
          /></el-form-item>
          <el-form-item prop="memberPhone" label="下单人手机号："
            ><el-input v-model.trim="filterForm.memberPhone" maxlength="20"
          /></el-form-item>
          <el-form-item prop="businessName" label="商户昵称"
            ><el-input
              v-model="filterForm.businessName"
              clearable
              maxlength="50"
          /></el-form-item>
          <el-form-item prop="mercNo" label="商户编号"
            ><el-input v-model="filterForm.mercNo" clearable maxlength="50"
          /></el-form-item>
          <el-form-item prop="type" label="配送方式：">
            <el-select
              v-model="filterForm.type"
              filterable
              placeholder="请选择配送方式"
              style="width: 400px"
            >
              <el-option label="全部类型" value />
              <el-option
                v-for="(v, k) in typeOptions"
                :key="k"
                :label="v"
                :value="k"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="pubOrderStatus" label="订单状态：">
            <el-select
              v-model="filterForm.pubOrderStatus"
              filterable
              placeholder="订单状态"
              style="width: 400px"
            >
              <el-option label="全部类型" value />
              <el-option
                v-for="(v, k) in orderOptions"
                :key="k"
                :label="v"
                :value="k"
              />
            </el-select>
          </el-form-item>
          <div>
            <el-form-item label=" ">
              <el-button type="primary" round @click="search()"
                >查 询</el-button
              >
              <el-button round @click="reset">重 置</el-button>
              <!-- <el-button round @click="downExcel">导出</el-button> -->
            </el-form-item>
          </div>
        </el-form>
      </div>
      <!-- 筛选条件 end -->
    </div>

    <!-- 表格 start -->
    <div class="table-container app-main-content-box">
      <el-table :data="list" style="width: 100%" border>
        <!-- <el-table-column prop="number" label="商户编号" align="center">
          <template slot-scope="scope">
            <div></div>
          </template>
        </el-table-column> -->
        <el-table-column
          prop="mercStoreNo"
          label="商户编号"
          min-width="130"
          align="center"
        />
        <el-table-column
          prop="orderNo"
          label="订单编号"
          align="center"
          min-width="130"
        />
        <el-table-column
          prop="totalNum"
          label="订单总额（元）"
          min-width="100"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              {{
                scope.row.totalFee
                  ? (scope.row.totalFee / 100).toFixed(2)
                  : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="data1" label="实付金额（元）" align="center">
          <template slot-scope="scope">
            {{
              scope.row.payFee ? (scope.row.payFee / 100).toFixed(2) : "0.00"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="customerName" label="下单人昵称" align="center">
          <template slot-scope="scope">
            {{ scope.row.customerName || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="memberPhone" label="下单人手机号" align="center">
          <template slot-scope="scope">
            {{ scope.row.memberPhone || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="pubBusinessName" label="商户昵称" align="center">
          <template slot-scope="scope">
            {{ scope.row.pubBusinessName || "--" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="storeName"
          label="门店昵称"
          min-width="130"
          align="center"
        />
        <el-table-column prop="orderType" label="配送方式" align="center">
          <template slot-scope="scope">
            {{ typeOptions[scope.row.deliveryString] || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="订单状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.orderStatus || "--" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="下单时间"
          min-width="120"
          align="center"
        />
        <el-table-column label="操作" align="center" right>
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="lookDetail(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination-direction
        v-if="paginationConfig.total !== 0"
        :config="paginationConfig"
        @sizeschange="sizesChange"
        @pagechange="pageChange"
      />
      <!-- 列表 end -->
    </div>
    <HandleOrder ref="handleOrder" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { orderList } from "@/publicApi/publicDomain";
import HandleOrder from "./components/HandleOrder";
import { validateMobilePhone } from "@/utils/validate.js";

export default {
  components: {
    HandleOrder,
  },
  data() {
    const validateTelPhone = (rule, value, callback) => {
      if (value) {
        if (!validateMobilePhone(value)) {
          callback(new Error("请输入正确手机号"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      // 配置
      rules: {
        memberPhone: [
          {
            validator: validateTelPhone,
            trigger: ["blur", "change"],
          },
        ],
      },
      // 分页配置
      paginationConfig: {
        direction: "center", // 分页位置
        layout: "total, sizes, prev, pager, next, jumper", // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10, // 每页条数-默认10
      },
      typeOptions: {
        PUB_LOCAL: "外卖配送",
        PUB_HALL_FOOD: "堂食",
        PUB_PACK: "打包",
        PUB_SHOP_CONSUME: "到店消费",
        PUB_SELF_PICKUP: "自提",
      },

      orderOptions: {
        WAITING: "待支付",
        UNDERWAY: "进行中",
        FINISH: "已完成",
        CANCEL: "已取消",
        AFTER_SALES: "已售后",
      },
      isShowMoreFilter: true,
      loading: false,
      // 数据
      paymentMethods: [], // 所有的支付key
      lastPaymentMethodsArr: [], // 上一次选择
      filterForm: {
        createDate: [
          this.$momentMini().subtract(6, "days").format("YYYY-MM-DD") +
            " 00:00:00",
          this.$momentMini().format("YYYY-MM-DD") + " 23:59:59",
        ],
        orderNo: "",
        customerName: "",
        memberPhone: "",
        businessName: "",
        mercNo: "",
        type: "",
        pubOrderStatus: "",
      },
      list: [],
    };
  },
  mounted() {
    if (this?.$route?.query?.mercNo) {
      this.filterForm.mercNo = this?.$route?.query?.mercNo;
      this.filterForm.createDate = [];
    }

    this.fetchData();
  },
  methods: {
    /**
     * 数据组
     */
    fetchData(
      page = this.paginationConfig.page,
      pageSize = this.paginationConfig.pageSize
    ) {
      this.$refs.filterForm.validate(async (valid) => {
        if (valid) {
          let {
            filterForm: { createDate },
            filterForm,
            params,
            timeObj,
          } = this;
          createDate
            ? (timeObj = {
                beginTime: createDate[0],
                endTime: createDate[1],
              })
            : (timeObj = {
                beginTime: "",
                endTime: "",
              });
          params = Object.assign({}, filterForm, timeObj, {
            pageSize,
            pageNum: page,
          });
          delete params.createDate;
          this.loading = true;
          orderList(params)
            .then((res) => {
              this.list = res.data.list;
              this.paginationConfig.total = res.data.total;
              this.paginationConfig.page = res.data.pageNum;
            })
            .finally(() => {
              this.loading = false;
            });

          //
        }
      });
    },

    // 分页sizes改变
    sizesChange() {
      this.fetchData(1);
    },

    // 分页page改变
    pageChange() {
      this.fetchData();
    },

    /**
     * 按钮组
     */
    // 查询
    search(level) {
      // this.filterForm.level = level;
      this.fetchData(1);
    },

    // 重置
    reset() {
      this.$refs.filterForm.resetFields();
      (this.filterForm.createDate = [
        this.$momentMini().subtract(6, "days").format("YYYY-MM-DD") +
          " 00:00:00",
        this.$momentMini().format("YYYY-MM-DD") + " 23:59:59",
      ]),
        this.fetchData(1);
      // this.filterForm.paymentMethods = this.paymentMethods;
    },

    // 导出
    downExcel() {
      //   const data = this.fetchFilterParams();
      downMallOrdersList(data).then((res) => {
        if (res.code === 0) {
          this.$alert("数据导出中，请稍后到数据下载中心进行下载", "提示", {
            confirmButtonText: "确定",
            center: true,
            type: "success",
          });
        }
      });
    },
    lookDetail(row) {
      let { orderNo } = row;
      this.$refs.handleOrder.open({ orderNo }).then((res) => {
        this.fetchData();
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.order-manage-main {
  ::v-deep {
    .el-tabs__header {
      margin-bottom: 0;
      background-color: #fff;
      border-radius: 0 0 5px 5px;
    }

    .el-tabs__item {
      font-weight: bold;
    }

    .el-tabs__active-bar {
      height: 4px;
    }
  }

  .el-input,
  .el-select {
    width: 400px;
  }
}

/* 头部 */
.top-container {
  padding: 24px 30px 0;
  border-radius: 5px;
  background-color: #fff;
}

/* 筛选 */
.filter-container {
  margin: 20px 0 10px;
}

/* 表格 */
// .table-container {
//   margin-top: 10px;

//   ::v-deep {
//     .el-table tr:nth-of-type(odd) td {
//       //   border: none;
//     }

//     .el-table__row {
//       td:not(:first-child) {
//         vertical-align: top;
//       }
//     }

//     .el-table td {
//       border-bottom: 1px solid #dcdfe6;
//       font-weight: bold;
//     }
//   }
// }

/* 状态筛选 */
.status-tab-container {
  margin-bottom: 10px;
}

/* 表格内部通用盒子 */
.common-row-start-box {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 50px;
  margin-bottom: 12px;

  .cover-img {
    margin-right: 10px;
    flex: 0 0 50px;
    height: 50px;
    border: 1px solid #e7e7e7;
  }

  .goods-title {
    font-weight: bold;
    color: #000;
  }
}

.common-column-start-box {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 50px;
  margin-bottom: 12px;
}
</style>
